home *** CD-ROM | disk | FTP | other *** search
/ Clickx 75 / Clickx 75.iso / software / expressionweb / expressionwebv3 / ExpressionWeb_en.exe / Setup / WeLocen.cab / Microsoft.Expression.Web.Help.en / scripts / checkboxmenu.js next >
Encoding:
Text File  |  2009-06-19  |  4.6 KB  |  158 lines

  1.  
  2. function CheckboxMenu(id, data, persistkeys, globals)
  3. {
  4.     this.id = id;
  5.     this.menuCheckboxIds = new Array();
  6.     this.data = data;
  7.     this.count = 0;
  8.     
  9.     var element = document.getElementById(id);
  10.     var checkboxNodes = element.getElementsByTagName("input");
  11.  
  12.     for(var checkboxCount=0; checkboxCount < checkboxNodes.length; checkboxCount++)
  13.     {
  14.         var checkboxId = checkboxNodes[checkboxCount].getAttribute('id');
  15.         var checkboxData = checkboxNodes[checkboxCount].getAttribute('data');
  16.         var dataSplits = checkboxData.split(',');
  17.         var defaultValue = checkboxNodes[checkboxCount].getAttribute('value');
  18.         if (checkboxData != null && checkboxData.indexOf("persist") != -1)
  19.             persistkeys.push(checkboxId);
  20.         
  21.         this.menuCheckboxIds[dataSplits[0]] = checkboxId;
  22.         
  23.         // try to get the value for this checkbox id from globals
  24.         var persistedValue = (globals == null) ? null : globals.VariableExists(checkboxId) ? globals.VariableValue(checkboxId) : null;
  25.         var currentValue = (persistedValue != null) ? persistedValue : (defaultValue == null) ? "on" : defaultValue;
  26.         
  27.         // set the checkbox's check state
  28.         this.SetCheckState(checkboxId, currentValue);
  29.         
  30.         this.count++;
  31.     }
  32. }
  33.  
  34. CheckboxMenu.prototype.SetCheckState=function(id, value)
  35. {
  36.     var checkbox = document.getElementById(id);
  37.     if(checkbox != null)
  38.     {
  39.         checkbox.checked = (value == "on") ? true : false;
  40.     }
  41.     
  42.     // set the value for the checkbox id in the data array
  43.     this.data[id] = value;
  44. }
  45.  
  46. CheckboxMenu.prototype.GetCheckState=function(id)
  47. {
  48.     var checkbox = document.getElementById(id);
  49.     if(checkbox != null)
  50.         return checkbox.checked;
  51.     return false;
  52. }
  53.  
  54. CheckboxMenu.prototype.ToggleCheckState=function(id)
  55. {
  56.     // at least one checkbox must always be checked
  57.     var checkedCount = this.GetCheckedCount();
  58.         
  59.     if(this.data[id] == "on" && checkedCount > 1)
  60.         this.SetCheckState(id, "off");
  61.     else
  62.         this.SetCheckState(id, "on");
  63. }
  64.  
  65. // returns the checkbox id associated with a key
  66. CheckboxMenu.prototype.GetCheckboxId=function(key)
  67. {
  68.     return this.menuCheckboxIds[key];
  69. }
  70.  
  71. // returns the array of checkbox ids
  72. CheckboxMenu.prototype.GetCheckboxIds=function()
  73. {
  74.     return this.menuCheckboxIds;
  75. }
  76.  
  77. // returns the @data attribute of the checkbox element
  78. CheckboxMenu.prototype.GetCheckboxData=function(checkboxId)
  79. {
  80.     var checkbox = document.getElementById(checkboxId);
  81.     if (checkbox == null) return "";
  82.     return checkbox.getAttribute('data');
  83. }
  84.  
  85. CheckboxMenu.prototype.GetDropdownLabelId=function()
  86. {
  87.     var checkboxCount = this.count;
  88.     var checkedCount = this.GetCheckedCount();
  89.     var idPrefix = this.id;
  90.     
  91.     // if all boxes checked, use showall label
  92.     if (checkedCount == checkboxCount)
  93.         return idPrefix.concat("AllLabel");
  94.     
  95.     // if only one is checked, use label appropriate for that one checkbox
  96.     if (checkedCount == 1)
  97.     {
  98.         for(var key in this.menuCheckboxIds)
  99.         {
  100.             if (this.data[this.menuCheckboxIds[key]] == "on")
  101.             {
  102.                 return idPrefix.concat(key,'Label');
  103.             }
  104.         }
  105.     }
  106.     
  107.     // if multiple or zero checked, use multiple label
  108.     return idPrefix.concat("MultipleLabel");
  109. }
  110.  
  111. CheckboxMenu.prototype.GetCheckedCount=function()
  112. {
  113.     var count = 0;
  114.     for(var key in this.menuCheckboxIds)
  115.     {
  116.         if (this.data[this.menuCheckboxIds[key]] == "on")
  117.             count++;
  118.     }
  119.     return (count);
  120. }
  121.  
  122. // returns an array containing the ids of the checkboxes that are checked
  123. CheckboxMenu.prototype.GetCheckedIds=function()
  124. {
  125.     var idArray = new Array();
  126.     for(var key in this.menuCheckboxIds)
  127.     {
  128.         if (this.data[this.menuCheckboxIds[key]] == "on")
  129.             idArray.push(this.menuCheckboxIds[key]);
  130.     }
  131.     return idArray;
  132. }
  133.  
  134. CheckboxMenu.prototype.GetGroupCheckedCount=function(checkboxGroup)
  135. {
  136.     var count = 0;
  137.     for(var i = 0; i < checkboxGroup.length; i++)
  138.     {
  139.         if (this.data[checkboxGroup[i]] == "on")
  140.             count++;
  141.     }
  142.     return (count);
  143. }
  144.  
  145. CheckboxMenu.prototype.ToggleGroupCheckState=function(id, checkboxGroup)
  146. {
  147.     // at least one checkbox must always be checked
  148.     var checkedCount = this.GetGroupCheckedCount(checkboxGroup);
  149.     
  150.     // if the group has multiple checkboxes, one must always be checked; so toggle to "off" only if more than one currently checked
  151.     // if the group has only one checkbox, it's okay to toggle it on/off
  152.     if(this.data[id] == "on" && (checkedCount > 1 || checkboxGroup.length == 1))
  153.         this.SetCheckState(id, "off");
  154.     else
  155.         this.SetCheckState(id, "on");
  156. }
  157.  
  158.